package com.ywjh.inter.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ywjh.inter.entity.InsulinPenGlucoseEntity;
import com.ywjh.inter.entity.InsulinPenInjectEntity;
import com.ywjh.inter.entity.InsulinPenSysMsgEntity;
import com.ywjh.inter.service.InsulinPenGlucoseService;
import com.ywjh.inter.service.InsulinPenInjectService;
import com.ywjh.inter.service.InsulinPenSysMsgService;
import com.ywjh.inter.utils.StrUtil;

import net.sf.json.JSONObject;

public class Pen extends HttpServlet {

	private static final long serialVersionUID = -8492670704297170626L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html; charset=utf-8");
		
		JSONObject postData = StrUtil.getPostJSON(req);
		JSONObject message = postData.getJSONObject("message");
		String method = message.getString("method");

		int r = 1;
		String e = "";
		JSONObject json = new JSONObject();
		
		switch(method) {
			case "sys_msg":
				InsulinPenSysMsgEntity sysMsgEntity = new InsulinPenSysMsgEntity();
				sysMsgEntity.setSn(message.getString("sn"));
				sysMsgEntity.setSysTime(message.getString("time"));
				sysMsgEntity.setA(message.getString("a"));
				sysMsgEntity.setaStu(message.getString("a_stu"));
				sysMsgEntity.setB(message.getString("b"));
				sysMsgEntity.setbStu(message.getString("b_stu"));
				sysMsgEntity.setBat(message.getString("bat"));
				sysMsgEntity.setBoardVer(message.getString("board_ver"));
				sysMsgEntity.setVer(message.getString("ver"));
				InsulinPenSysMsgService.getInstance().add(sysMsgEntity);
				break;
			case "inject":
				InsulinPenInjectEntity injectEntity = new InsulinPenInjectEntity();
				injectEntity.setSn(message.getString("sn"));
				injectEntity.setTime(message.getString("time"));
				injectEntity.setChn(message.getString("chn"));
				injectEntity.setDrugId(message.getString("drug_id"));
				injectEntity.setVlu(message.getString("vlu"));
				InsulinPenInjectService.getInstance().add(injectEntity);
				break;
			case "glucose":
				InsulinPenGlucoseEntity glucoseEntity = new InsulinPenGlucoseEntity();
				glucoseEntity.setSn(message.getString("sn"));
				glucoseEntity.setTime(message.getString("time"));
				glucoseEntity.setVlu(message.getString("vlu"));
				InsulinPenGlucoseService.getInstance().add(glucoseEntity);
				break;
			default:
				r = 0;
				e = "unknown method";
				System.out.println("unknown method: " + postData);
		}
		json.put("r", r);
		json.put("e", e);
		PrintWriter pw = resp.getWriter();
		pw.write(json.toString());
		pw.flush();
		pw.close();
	}
}
